package com.dxy.chatserver.db.entity;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alicloud.openservices.tablestore.model.*;

public class OnlineMessageBuild {

    /**
     * 数据库名字
     */
    public final static String DB_NAME = "online_message";

    /**
     * key
     */
    public static final String sessionId = "sessionId";
    public static final String msgId = "msgId";
    public static final String createTime = "createTime";

    /**
     * Column
     */

    public static final String type = "type";
    public static final String content = "content";
    public static final String isRoomChat = "isRoomChat";
    public static final String status = "status";

    public static final String fromUser = "fromUser";
    public static final String toUser = "toUser";
    public static final String bodies = "bodies";


    /**
     * 构建的主键
     * @param response
     * @return
     */
    public static PrimaryKey createPrimaryKeyBuilder(AcceptLineMsgResponse response){
        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.sessionId, PrimaryKeyValue.fromString(response.getSessionId()));
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.msgId, PrimaryKeyValue.fromString(response.getMsgId()));
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.createTime, PrimaryKeyValue.fromString(String.valueOf(response.getCreateTime())));
        return primaryKeyBuilder.build();
    }

    /**
     * 构建范围查询主键 最小值
     * @param sessionId
     * @param msgId
     * @param createTime
     * @return
     */
    public static PrimaryKey createPrimaryKeyRangeMin(String sessionId,String msgId,String createTime){
        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.sessionId, ObjectUtil.isNotEmpty(sessionId) ? PrimaryKeyValue.fromString(sessionId) : PrimaryKeyValue.INF_MIN);
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.msgId, ObjectUtil.isNotEmpty(msgId) ? PrimaryKeyValue.fromString(msgId) : PrimaryKeyValue.INF_MIN);
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.createTime, ObjectUtil.isNotEmpty(msgId) ? PrimaryKeyValue.fromString(String.valueOf(createTime)) : PrimaryKeyValue.INF_MIN);
        return primaryKeyBuilder.build();
    }

    /**
     * 构建范围查询主键 最大值
     * @param sessionId
     * @param msgId
     * @param createTime
     * @return
     */
    public static PrimaryKey createPrimaryKeyRangeMax(String sessionId,String msgId,String createTime){
        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.sessionId, ObjectUtil.isNotEmpty(sessionId) ? PrimaryKeyValue.fromString(sessionId) : PrimaryKeyValue.INF_MAX);
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.msgId, ObjectUtil.isNotEmpty(msgId) ? PrimaryKeyValue.fromString(msgId) : PrimaryKeyValue.INF_MAX);
        primaryKeyBuilder.addPrimaryKeyColumn(OnlineMessageBuild.createTime, ObjectUtil.isNotEmpty(msgId) ? PrimaryKeyValue.fromString(String.valueOf(createTime)) : PrimaryKeyValue.INF_MAX);
        return primaryKeyBuilder.build();
    }

    /**
     * 构建写入的列
     * @param rowPutChange
     * @param response
     * @return
     */
    public static RowPutChange createRowPutChange(RowPutChange rowPutChange, AcceptLineMsgResponse response){
        rowPutChange.addColumn(new Column(OnlineMessageBuild.content, ColumnValue.fromString(response.getContent())));
        rowPutChange.addColumn(new Column(OnlineMessageBuild.type, ColumnValue.fromLong(response.getType())));
        rowPutChange.addColumn(new Column(OnlineMessageBuild.isRoomChat, ColumnValue.fromLong(response.getIsRoomChat())));
        rowPutChange.addColumn(new Column(OnlineMessageBuild.status, ColumnValue.fromLong(response.getStatus())));

        rowPutChange.addColumn(new Column(OnlineMessageBuild.fromUser, ColumnValue.fromString(JSONUtil.parse(response.getFromUser()).toString())));
        rowPutChange.addColumn(new Column(OnlineMessageBuild.toUser, ColumnValue.fromString(JSONUtil.parse(response.getToUser()).toString())));
        rowPutChange.addColumn(new Column(OnlineMessageBuild.bodies, ColumnValue.fromString(JSONUtil.parse(response.getBodies()).toString())));
        return rowPutChange;
    }

}
